﻿using otis.entities.products;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace otis.views.product
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class Product : Page
    {
        List<Campaign> campaigns = CampaignHelper.InitCampaign();
        List<UseRange> useRanges = UseRangeHelper.InitUseRange();

        public Product()
        {
            this.InitializeComponent();

            
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //base.OnNavigatedTo(e);

            this.lvCampaign.ItemsSource = campaigns;
            this.lvCampaign.SelectedIndex = 1;
            

        }

        private void btnCampaign_Click(object sender, RoutedEventArgs e)
        {
            if (this.lvCampaign.Visibility == Windows.UI.Xaml.Visibility.Collapsed)
            {
                this.lvCampaign.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.lvCampaign.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        private void lvCampaign_ItemClick(object sender, ItemClickEventArgs e)
        {
            Campaign item = e.ClickedItem as Campaign;
            if (item == null) { 
                //清除数据
                return;
            }

            //绑定数据
            List<entities.products.Product> items = ProductHelper.InitProduct(this.campaigns, this.useRanges);
            items = (from p in items where p.Campaign.Name.Equals(item.Name) select p).ToList<entities.products.Product>();
            
            this.cvsProduct.Source = items;
        }


    }
}
